package com.superMall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.sun.corba.se.spi.activation.Server;
import com.superMall.common.ServerResponse;
import com.superMall.dao.ShippingMapper;
import com.superMall.pojo.Shipping;
import com.superMall.service.IShippingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by guanwei on 2017/11/21.
 */
@Service("iShippingService")
public class ShippingServiceImpl implements IShippingService{


    @Autowired
    private ShippingMapper shippingMapper;


    public ServerResponse<Map> add(Shipping shipping,Integer userId){
        shipping.setUserId(userId);
        int rowCount = shippingMapper.insert(shipping);
        if(rowCount > 0){
            Map result = Maps.newHashMap();
            result.put("shippingId",shipping.getId());
            return ServerResponse.createBySuccess("修改地址成功",result);
        }
        return ServerResponse.createByErrorMessage("修改地址失败");
    }

    public ServerResponse del(Integer userId, Integer shippingId){
        int rowCount = shippingMapper.deleteByUserIdAndShippingId(userId, shippingId);
        if(rowCount > 0){
            return ServerResponse.createBySuccessMessage("删除地址成功");
        }
        return ServerResponse.createBySuccessMessage("删除地址失败");
    }

    public ServerResponse update(Shipping shipping,Integer userId){
        shipping.setUserId(userId);
        int rowCount = shippingMapper.updateByShipping(shipping);
        if(rowCount > 0){
            return ServerResponse.createBySuccessMessage("修改地址成功");
        }
        return ServerResponse.createBySuccessMessage("修改地址失败");
    }

    public ServerResponse<Shipping> select(Integer userId, Integer shippingId){
        Shipping shipping = shippingMapper.selectByUserIdAndShippingId(userId, shippingId);
        if(shipping == null){
            return ServerResponse.createBySuccessMessage("未查询到该地址");
        }
        return ServerResponse.createBySuccess(shipping);
    }

    public ServerResponse<PageInfo> list(Integer userId,Integer pageNum,Integer pageSize){
        List<Shipping> shippingList = shippingMapper.selectByUserId(userId);
        PageHelper.startPage(pageNum,pageSize);
        PageInfo pageResult = new PageInfo(shippingList);
        return ServerResponse.createBySuccess(pageResult);
    }
}
